var vm = new Vue({
    el: '#rrapp',
    data: {
        title: '',
        userRegisterSource: [],
        couponList: [],         //优惠券信息
        orderCouponIds: [],     //定义首单优惠券id
        registerCouponIds: [],  //定义注册优惠券id
        q: {sourceName: '', sendCoupon: ''}, /*查询字段  query*/
        page: {currentPage: 0, pageSizes: [20, 50, 100], pageSize: 20, total: 0},
        //新增编辑
        RegisterFrom: {
            sourceName: '', sourceKey: '', sourceUrl: '', startTime: '',
            endTime: '', sendCoupon: '', orderCouponIds: '', registerCouponIds: ''
        },
        RegisterVisible: false,
        resetForm(RegisterFrom) {
            this.$refs[RegisterFrom].resetFields();
        }
    },
    created: function () { //加载数据
        this.loadData();
    },
    methods: {
        query: function () {
            this.page.currentPage = 1;
            this.loadData();
        },
        //恢复按钮
        reset: function () {
            vm.q.sourceName = '';
            vm.q.sendCoupon = '';
            this.loadData();
        },
        //获取后台数据
        loadData: function () {
            var params = this.q;
            // 分页参数
            params.page = this.page.currentPage;
            params.limit = this.page.pageSize;
            $.ajax({
                type: "POST",
                url: baseURL + "userRegisterSource/userregistersource/list",
                contentType: "application/json;charset=UTF-8",
                data: JSON.stringify(params),
                success: function (r) {
                    if (r.code == 0) {
                        vm.userRegisterSource = r.page.list;
                        vm.page.total = r.page.totalCount;
                        vm.couponList = r.couponList.couponList;
                        // console.log(vm.couponList)
                    } else {
                        alert(r.msg);
                    }
                }
            })
        },
        //每页显示数据量变更
        handleSizeChange: function (val) {
            vm.page.pageSize = val;
            this.loadData();
        },
        //当前页面
        handleCurrentChange: function (val) {
            vm.page.currentPage = val;
            this.loadData();
        },
        //序号
        indexMethod: function (index) {
            return index + 1;
        },
        add: function () {
            vm.title = "新 增";
            vm.RegisterFrom = {
                sourceName: '', sourceKey: '', sourceUrl: '', startTime: '',
                endTime: '', sendCoupon: '', orderCouponIds: [], registerCouponIds: []
            };
            vm.RegisterVisible = true
        },
        handleEdit: function (index, row) {
            vm.title = "修 改";
            var rowData = JSON.parse(JSON.stringify(row)); //行数据
            var dataStr = rowData.orderCouponIds;//原始字符串
            var dataStrRe = rowData.registerCouponIds;
            //字符串数组装换整型数组
            if (!isBlank(dataStr) && !isBlank(dataStrRe)) {
                var dataStrArr = dataStr.split(",");//分割成字符串数组
                var dataStrArrRe = dataStrRe.split(",");
                rowData.orderCouponIds = dataStrArr.map(function (data) {
                    return +data;
                });
                rowData.registerCouponIds = dataStrArrRe.map(function (data) {
                    return +data;
                });
            } else {
                rowData.registerCouponIds = [];
                rowData.orderCouponIds = [];
            }
            vm.RegisterFrom = Object.assign({}, rowData);
            vm.RegisterVisible = true;
        },
        //编辑取消清空表单
        closeChangeMachie() {
            vm.RegisterVisible = false;
            this.$refs['RegisterFrom'].resetFields();
        },
        //新增编辑提交
        submitFrom(RegisterFrom) {
            // debugger
            var postData = {
                sourceId: vm.RegisterFrom.sourceId, sourceName: vm.RegisterFrom.sourceName,
                sourceKey: vm.RegisterFrom.sourceKey, sourceUrl: vm.RegisterFrom.sourceUrl,
                startTime: vm.RegisterFrom.startTime, endTime: vm.RegisterFrom.endTime,
                sendCoupon: vm.RegisterFrom.sendCoupon, orderCouponIds: vm.RegisterFrom.orderCouponIds,
                registerCouponIds: vm.RegisterFrom.registerCouponIds
            };
            var orderIds = [];
            var register = [];
            var registerCouponIds = vm.RegisterFrom.registerCouponIds;
            var orderCouponIds = vm.RegisterFrom.orderCouponIds;
            for (var i = 0; i < orderCouponIds.length; i++) {
                orderIds.push(orderCouponIds[i]);
            }
            for (var j = 0; j < registerCouponIds.length; j++) {
                register.push(registerCouponIds[j]);
            }
            postData.orderCouponIds = orderIds.join(",");  //数据','断开
            postData.registerCouponIds = register.join(",");
            var url;
            var _this = this;
            if (isBlank(postData.sourceId)) {
                url = "userRegisterSource/userregistersource/save";
            } else {
                url = "userRegisterSource/userregistersource/update"
            }
            this.$refs[RegisterFrom].validate(function (valid) {
                if (valid) {
                    $.ajax({
                        type: "POST",
                        url: baseURL + url,
                        contentType: "application/json",
                        data: JSON.stringify(postData),
                        success: function (r) {
                            if (r.code === 0) {
                                vm.RegisterVisible = false;
                                _this.$message({
                                    type: 'success',
                                    message: '操作成功!'
                                }, 200);
                                _this.loadData();
                            } else {
                                alert(r.msg);
                            }
                        }
                    });
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        //删除
        handleDelete: function (index, row) {
            var sourceId = row.sourceId;
            var _this = this;
            this.$confirm('此操作将删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(function () {
                $.ajax({
                    type: "GET",
                    url: baseURL + "userRegisterSource/userregistersource/delete?sourceId=" + sourceId,
                    contentType: "application/json",
                    data: null,
                    success: function (r) {
                        if (r.code == 0) {
                            _this.$message({
                                type: 'success',
                                message: '删除成功!'
                            }, 200);
                            _this.loadData();
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            }).catch(function () {
                _this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            })
        }
    }
});